package logic

import (
	"common/model"
	"context"
	"fmt"

	product "product_server/proto"
)

func (p *ProductServer) GetGroupDetail(ctx context.Context, req *product.GetGroupDetailRequest) (*product.GetGroupDetailResponse, error) {
	// 获取拼团详细信息
	gb := model.GroupBuy{}
	if err := gb.Detail(int(req.GroupId)); err != nil {
		return nil, fmt.Errorf("获取拼团详细信息失败: %w", err)
	}

	// 获取发起人信息
	u := model.User{}
	creator, err := u.GetUserAccountInfoByUid(int64(gb.CreatorId))
	if err != nil {
		return nil, fmt.Errorf("获取发起人信息失败: %w", err)
	}

	// 获取成员头像
	var memberAvatar []string
	for _, id := range gb.MemberIds {
		memberInfo, err := u.GetUserAccountInfoByUid(int64(id))
		if err != nil {
			return nil, fmt.Errorf("获取成员信息失败: %w", err)
		}
		memberAvatar = append(memberAvatar, memberInfo.Avatar)
	}

	return &product.GetGroupDetailResponse{
		CreatorAvatar: creator.Avatar,
		MemberAvatar:  memberAvatar,
		CurrentNum:    int64(gb.CurrentSize),
		Price:         float32(gb.Price),
		Status:        gb.Status,
	}, nil
}
